package cn.edu.zjgsu.ito.controller;
import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import cn.edu.zjgsu.ito.service.AdminExportScoreService;
import cn.edu.zjgsu.ito.service.AdminExportStudService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;




import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

@Before(AdminControllerInterceptor.class)
public class AdminExportStudController extends Controller {
	/*
	 * 刘阳
	 * 2017/11/01
	 */
    AdminExportStudService adminExportStudService =ServiceFactory.createAdminExportStudService();
	private int passportNo;
	private List<Record> records;
	private Record record;
	private String aa;
	public static final DateFormat formatter=new SimpleDateFormat("yyyy-MM-dd-HH：mm");
	
	/**
	  *  导出Excel
	 * @throws FileNotFoundException 
	  * 
	  */
 	public void exportExcel() throws FileNotFoundException
 	{

 		String aa=getPara("passportNo");
 		String a="'"+aa+"'";
 		a=a.replaceAll(",","','");
 		records=adminExportStudService.Student(a);
 		System.out.println("777777777777777777    "+records);

 			Date d=new java.util.Date();
 	 		

 	 		String filedir=PathKit.getWebRootPath();
 	 		filedir=filedir+"\\WEB-INF\\download\\";
 	 		
 	        deleteFile(new File(filedir));//清空缓存区
 	         
 	 		String firstName;
 	 		
 	 		
// 	 		records=adminExportStudService.Student(bb[i]);
 	 		firstName=records.get(0).get("firstName");
 	 		firstName=firstName.replace(" ", "-");
 	 		firstName=firstName+"学生汇总表("+formatter.format(d)+").xls";
 	 		
 	 		OutputStream out = new FileOutputStream(filedir+firstName);	 		
 	 		System.out.println(filedir+firstName);
 	 		adminExportStudService.exportExcel(records, out);
 	 		String outpath=filedir+firstName;
 	 		
 	 		renderFile(new File(outpath));
 		}
 		

 	
 	 public void deleteFile(File oldPath) {
          if (oldPath.isDirectory()) {
           File[] files = oldPath.listFiles();
           for (File file : files) {
             deleteFile(file);
           }
          }else{
            oldPath.delete();
          }
        }

}
